package neo.app.action;

import java.util.Map;

import neo.core.Constants;
import neo.core.common.PagingList;
import neo.core.util.MD5;

public class UserAction extends BaseAction {
	private static final String NOT_EXISTS = "该用户不存在";// 用户不存在
	private static final String WRONG_PASS = "密码错误";// 密码错误
	private String userName;
	private String password;
	private String indexUrl;
	private String errorInfo = null;
	private PagingList users;
	private PagingList employees;

	/**
	 * 用户登录
	 */
	public String login() throws Exception {
		Map dbUser = getServMgr().getUserService().getUserByUserName(userName);
		MD5 m = new MD5();
		String md5Pwd = m.getMD5ofStr(password.trim());
		if (dbUser != null && md5Pwd.equals((String) dbUser.get("user_password"))) {
			//读取用户权限
			int userID = Integer.parseInt(dbUser.get("user_id").toString());
			Map authMap = getServMgr().getUserService().getAuth(userID);
			indexUrl = (String) getSession().remove(Constants.ORIGINAL_URL);
			getSession().clear();// 清空原始Session信息
			getSession().put(Constants.LOGIN_USER, dbUser);
			getSession().put(Constants.LOGIN_AUTH, authMap);
//	log.debug(authMap +" UserAction");		
			if (indexUrl == null) {
				return "homepage";
			}
			return SUCCESS;
		}

		if (dbUser == null) {
			errorInfo = NOT_EXISTS;
			return ERROR;
		}
		if (!password.equals((String) dbUser.get("user_password"))) {
			errorInfo = WRONG_PASS;
			return ERROR;
		}
		return ERROR;
	}

	/**
	 * 用户登出
	 */
	public String logout() throws Exception {
		getSession().remove(Constants.LOGIN_USER);
		getSession().remove(Constants.ORIGINAL_URL);
		getSession().clear();
		return SUCCESS;
	}
	
	
	//----------------用户维护--------------------------
	/**
	 * 用户列表
	 */
	public String list() throws Exception {
		users = getServMgr().getUserService().getAllUsers();
		return "list";
	}
	
	/**
	 * 添加用户准备 
	 * @return
	 */
	public String addPre(){
		employees = getServMgr().getEmployeeService().getAllEmployee();
		return "addPre";
	}
	
	/**
	 * 添加用户
	 * @return
	 */
	public String add(){
		if(getServMgr().getUserService().addUser(getParameters())){
			addMessage("添加用户成功");
			addRedirURL("返回用户列表", "user!list.action");
		}else{
			addMessage("操作失败");
			addRedirURL("返回", "@back");
		}
		return EXECUTE_RESULT;
	}
	
	
	
	
	
	
	//----------getter and setter methods------------
	public String getIndexUrl() {
		return indexUrl;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public String getUserName() {
		return userName;
	}

	public String getErrorInfo() {
		return errorInfo;
	}

	public PagingList getUsers() {
		return users;
	}

	public PagingList getEmployees() {
		return employees;
	}

	public void setEmployees(PagingList employees) {
		this.employees = employees;
	}

	
}